package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.mapper.AttachmentMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;

import com.yqg.education.service.IAttachmentService;
import com.yqg.education.entity.Attachment;

import org.springframework.web.bind.annotation.RestController;

/**
 * AttachmentController 前端控制器
 *
 * @author zxf
 * @since 2025-09-08
 */
@RestController
@RequestMapping("/attachment")
public class AttachmentController {

    @Resource
    private IAttachmentService attachmentService;

    @Resource
    private AttachmentMapper attachmentMapper;
    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody Attachment attachment) {
        return Result.success(attachmentService.saveOrUpdate(attachment));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Long id) {
        Attachment attachment = attachmentService.getById(id);
        return Result.success(attachmentService.removeById(attachment));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Attachment> queryWrapper = new QueryWrapper<>();
        List<Attachment> list = attachmentService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(attachmentService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String correlationTable,
                           @RequestParam Long correlationId) {
        LambdaQueryWrapper<Attachment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attachment::getCorrelationTable, correlationTable)
                .eq(Attachment::getCorrelationId, correlationId)
                .orderByDesc(Attachment::getId);
        return Result.success(attachmentService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


    /**
     * 获取对应关联ID下的相关附件
     *
     * @param correlationTable 关联表名
     * @param correlationId    关联id
     */
    @GetMapping("/getAttachment")
    public Result getAttachment(
            @RequestParam String correlationTable,
            @RequestParam Long correlationId) {
        Map<String, Object> map = new HashMap<>();
        map.put("records", attachmentMapper.getAttachmentByCorrelationId(correlationTable,correlationId));
        return Result.success(map);
    }

}

